package com.mediacenter.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.mediacenter.R;
import com.mediacenter.R.id;
import com.mediacenter.R.layout;
import com.mediacenter.R.string;
import com.mediacenter.R.style;
import com.mediacenter.adapter.MyFragmentPagerAdapter;
import com.mediacenter.fragment.HKTWTVFragment;
import com.mediacenter.fragment.LocalTVFragment;
import com.mediacenter.fragment.SatelliteTVFragment;
import com.mediacenter.fragment.CCTVFragment;
import com.mediacenter.utils.AnimTabLayout;
import com.mediacenter.utils.AnimTabLayout.OnTabChangeListener;

public class OnlineTVActivity extends BaseActivity implements OnPageChangeListener,
		OnTabChangeListener {
	private String TAG = "OnlineTVActivity";
	private ViewPager mViewPager;
	private List<String> mTitles;
	public static int mScreenWidth;
	private AnimTabLayout mAnimTab;
	private ArrayList<Fragment> fragmentsList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_mediacenteractivity);
		mScreenWidth = getWindowManager().getDefaultDisplay().getWidth();
		initView();
	}
	@Override
	protected void onResume() {
		super.onResume();
	}
	@Override
	protected void onPause() {
		super.onPause();
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	

	BaseAdapter tabAdapter = new BaseAdapter() {

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = new TextView(OnlineTVActivity.this);
			}
			((TextView) convertView).setText(mTitles.get(position));
			((TextView) convertView).setTextAppearance(OnlineTVActivity.this,R.style.tvTitle);
			return convertView;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public int getCount() {
			return mTitles.size();
		}
	};

	private void initView() {
		mViewPager = (ViewPager) findViewById(R.id.viewpager);
		fragmentsList = new ArrayList<Fragment>();
		Fragment CCTvFragment = CCTVFragment.newInstance("01");
		Fragment SatelliteTvFragment = SatelliteTVFragment.newInstance("02");
		Fragment LocalTvFragment = LocalTVFragment.newInstance("03");
		Fragment HKTWTvFragment = HKTWTVFragment.newInstance("04");
		fragmentsList.add(CCTvFragment);
		fragmentsList.add(SatelliteTvFragment);
		fragmentsList.add(LocalTvFragment);
		fragmentsList.add(HKTWTvFragment);
		mViewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentsList));
		mViewPager.setCurrentItem(0);
		mViewPager.setOnPageChangeListener(this);

		mTitles = new ArrayList<String>();
		mTitles.add(getString(R.string.cctv_browser));
		mTitles.add(getString(R.string.satellitetv_browser));
		mTitles.add(getString(R.string.localtv_browser));
		mTitles.add(getString(R.string.hktwtv_browser));

		mAnimTab = (AnimTabLayout) findViewById(R.id.animTab);
		mAnimTab.setBackgroundResource(R.drawable.topbar_bg);
		mAnimTab.setAdapter(tabAdapter);
		mAnimTab.setOnTabChangeListener(this);
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		Log.i(TAG, "newConfig========>" + newConfig);
		super.onConfigurationChanged(newConfig);
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	@Override
	public void onPageSelected(int index) {
		onPageListener(index);
		index = index % fragmentsList.size();
		mAnimTab.moveTo(index);
	}

	
	@Override
	protected void onPageListener(int index) {
		super.onPageListener(index);
	}
	@Override
	public void tabChange(int index) {
		int curr = mViewPager.getCurrentItem();
		int realIndex = curr % fragmentsList.size();
		int toIndex = curr + (index - realIndex);
		Log.i(TAG, "index:" + index + " curr:" + curr + " realIndex:" + realIndex + " toIndex:" + toIndex);
		mViewPager.setCurrentItem(toIndex, false);
	}
}
